package com.sinothk.fc.reportform.service.impl;

import cn.hutool.core.date.DateUtil;
import com.sinothk.fc.reportform.entity.*;
import com.sinothk.fc.reportform.mapper.ReportFormMapper;
import com.sinothk.fc.reportform.service.IReportFormService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ReportFormServiceImpl implements IReportFormService {

    @Resource
    private ReportFormMapper reportFormMapper;

    @Override
    public ReportFormForDay reportFormForDay(ReportFormReq req) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        Date now = new Date();
        req.setStoreId(loginUser.getStoreId());
        req.setDateFlag(DateUtil.format(now, "yyyy年MM月dd日"));

        // 收入及支出
        ReportFormForDay reportFormForDay = new ReportFormForDay();
        InOutComeReportFormBean inOutCome = reportFormMapper.getInOutCome(req);
        if (inOutCome != null) {
            inOutCome.setProfit(inOutCome.getIncome() - inOutCome.getOutcome());
        }
        reportFormForDay.setInOutComeBean(inOutCome);
        // 获取当天营业额
        ReportFormReq req2 = new ReportFormReq();
        req2.setStoreId(loginUser.getStoreId());
        req2.setDateFlag(DateUtil.format(now, "yyyy-MM-dd"));
        List<ReportLineDataBean> dayHoursSaleIncomeList = reportFormMapper.reportDayHoursSaleIncome(req2);
        reportFormForDay.setSaleIncomeList(dayHoursSaleIncomeList);

        // 上线状态
        ReportFormForGoodsOnlineState goodsOnlineState = reportFormMapper.reportGoodsOnlineState(req2);
        reportFormForDay.setGoodsOnlineState(goodsOnlineState);

        // 各商品销售利润
        req2.setDateFormat("%Y-%m-%d");
        List<ReportLineDataBean> saleProfitList = reportFormMapper.reportSaleProfitList(req2);
        reportFormForDay.setSaleGoodsProfitList(saleProfitList);

        // 商品销售数量
        req2.setDateFormat("%Y-%m-%d");
        List<ReportLineDataBean> daySaleNumList = reportFormMapper.reportDaySaleNumList(req2);
        reportFormForDay.setDaySaleNumList(daySaleNumList);

        // 商品库存过期统计
        ReportStockLimitDateBean stockLimitDate = reportFormMapper.reportStockLimitDate(req2);
        reportFormForDay.setStockLimitDateBean(stockLimitDate);

        // 商品物料过期统计
        ReportStockLimitDateBean materialLimitDate = reportFormMapper.reportMaterialLimitDate(req2);
        reportFormForDay.setMaterialLimitDateBean(materialLimitDate);
        return reportFormForDay;
    }

    @Override
    public ReportFormForMonth reportFormForMonth() throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");

        Date now = new Date();
        ReportFormReq reportFormReq = new ReportFormReq();
        reportFormReq.setStoreId(loginUser.getStoreId());
        reportFormReq.setDateFlag(DateUtil.format(now, "yyyy年MM月"));

        // 收入及支出
        ReportFormForMonth reportForm = new ReportFormForMonth();
        InOutComeReportFormBean inOutCome = reportFormMapper.getInOutComeForMonth(reportFormReq);
        if (inOutCome != null) {
            inOutCome.setProfit(inOutCome.getIncome() - inOutCome.getOutcome());
        }
        reportForm.setInOutComeBean(inOutCome);

        // 获取每天营业额
        ReportFormReq req2 = new ReportFormReq();
        req2.setStoreId(loginUser.getStoreId());
        req2.setDateFlag(DateUtil.format(now, "yyyy-MM"));
        List<ReportLineDataBean> dayHoursSaleIncomeList = reportFormMapper.reportMonthDaySaleIncome(req2);
        reportForm.setSaleIncomeList(dayHoursSaleIncomeList);

        // 获取每天支出
        List<ReportLineDataBean> dayHoursSaleOutcomeList = reportFormMapper.reportMonthDaySaleOutcome(req2);
        reportForm.setSaleOutcomeList(dayHoursSaleOutcomeList);

        // 各商品销售利润
        req2.setDateFormat("%Y-%m");
        List<ReportLineDataBean> saleProfitList = reportFormMapper.reportSaleProfitList(req2);
        reportForm.setSaleGoodsProfitList(saleProfitList);

        // 商品销售数量
        req2.setDateFormat("%Y-%m");
        List<ReportLineDataBean> saleNumList = reportFormMapper.reportDaySaleNumList(req2);
        reportForm.setSaleNumList(saleNumList);

        // 物料消耗记录统计
        List<ReportLineDataBean> materialCostNumList = reportFormMapper.reportMaterialCostNumList(req2);
        reportForm.setMaterialCostNumList(materialCostNumList);

        return reportForm;
    }

    @Override
    public ReportFormForYear reportFormForYear() throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        Date now = new Date();
        String yearStr = DateUtil.format(now, "yyyy");

        ReportFormReq reportFormReq = new ReportFormReq();
        reportFormReq.setStoreId(loginUser.getStoreId());
        reportFormReq.setDateFlag(yearStr);

        // 收入及支出
        ReportFormForYear reportForm = new ReportFormForYear();
        InOutComeReportFormBean inOutCome = reportFormMapper.getInOutComeForYear(reportFormReq);
        if (inOutCome != null) {
            inOutCome.setProfit(inOutCome.getIncome() - inOutCome.getOutcome());
        }
        reportForm.setInOutComeBean(inOutCome);

        // 获取每月营业额
        List<ReportLineDataBean> dayHoursSaleIncomeList = reportFormMapper.reportYearMonthSaleIncome(reportFormReq);
        reportForm.setSaleIncomeList(dayHoursSaleIncomeList);
        // 获取每月支出
        List<ReportLineDataBean> dayHoursSaleOutcomeList = reportFormMapper.reportYearMonthSaleOutcome(reportFormReq);
        reportForm.setSaleOutcomeList(dayHoursSaleOutcomeList);

        // 各商品销售利润
        reportFormReq.setDateFormat("%Y");
        List<ReportLineDataBean> saleProfitList = reportFormMapper.reportSaleProfitList(reportFormReq);
        reportForm.setSaleGoodsProfitList(saleProfitList);

        // 商品销售数量
        reportFormReq.setDateFormat("%Y");
        List<ReportLineDataBean> daySaleNumList = reportFormMapper.reportDaySaleNumList(reportFormReq);
        reportForm.setSaleNumList(daySaleNumList);

        // 物料消耗记录统计
        List<ReportLineDataBean> materialCostNumList = reportFormMapper.reportMaterialCostNumList(reportFormReq);
        reportForm.setMaterialCostNumList(materialCostNumList);

        return reportForm;
    }

    @Override
    public ReportFormFor3Year reportFormFor3Year() throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        Date now = new Date();
        String thisYear = DateUtil.format(now, "yyyy");
        ReportFormReq reportFormReq = new ReportFormReq();
        reportFormReq.setStoreId(loginUser.getStoreId());
        reportFormReq.setDateFlag(thisYear);

        ReportFormFor3Year reportForm = new ReportFormFor3Year();
        // 收入及支出
        InOutComeReportFormBean inOutCome = reportFormMapper.getInOutComeForYear(reportFormReq);
        if (inOutCome != null) {
            inOutCome.setProfit(inOutCome.getIncome() - inOutCome.getOutcome());
        } else {
            inOutCome = new InOutComeReportFormBean();
        }
        int leastYear = Integer.parseInt(thisYear) - 1;
        int leastTwoYear = Integer.parseInt(thisYear) - 2;
        reportFormReq.setDateFlag(String.valueOf(leastYear));
        InOutComeReportFormBean inOutComeLeastYear = reportFormMapper.getInOutComeForYear(reportFormReq);
        if (inOutComeLeastYear != null) {
            inOutComeLeastYear.setProfit(inOutComeLeastYear.getIncome() - inOutComeLeastYear.getOutcome());
        } else {
            inOutComeLeastYear = new InOutComeReportFormBean();
        }
        long incomeDiff = inOutCome.getIncome() - inOutComeLeastYear.getIncome();
        long outcomeDiff = inOutCome.getOutcome() - inOutComeLeastYear.getOutcome();
        long profitDiff = inOutCome.getProfit() - inOutComeLeastYear.getProfit();

        float incomePercent = inOutCome.getIncome() == 0 ? 0 : (incomeDiff * 1.0f) / (inOutCome.getIncome() * 1.0f);
        float outcomePercent = inOutCome.getOutcome() == 0 ? 0 :  (outcomeDiff * 1.0f) / (inOutCome.getOutcome() * 1.0f);
        float profitPercent =  inOutCome.getProfit() == 0 ? 0 : (profitDiff * 1.0f) / (inOutCome.getProfit() * 1.0f);
        ReportFormFor3YearInOutcome formFor3YearInOutcome = new ReportFormFor3YearInOutcome(incomePercent, outcomePercent, profitPercent);
        reportForm.setInOutProfitCome(formFor3YearInOutcome);

        // ----------------------------------------------- 当年营收及成本
        // 获取每月营业额
        ReportFormReq req1 = new ReportFormReq();
        req1.setStoreId(loginUser.getStoreId());
        req1.setDateFlag(thisYear);
        List<ReportLineDataBean> dayHoursSaleIncomeList = reportFormMapper.reportYearMonthSaleIncome(req1);
        reportForm.setSaleIncomeList(dayHoursSaleIncomeList);
        // 获取每月支出
        List<ReportLineDataBean> dayHoursSaleOutcomeList = reportFormMapper.reportYearMonthSaleOutcome(req1);
        reportForm.setSaleOutcomeList(dayHoursSaleOutcomeList);
        // 获取每月利润
        List<ReportLineDataBean> saleProfitList = reportFormMapper.reportYearMonthSaleProfit(req1);
        reportForm.setSaleProfitList(saleProfitList);

        // ----------------------------------------------- 前一年营收及成本
        // 获取每月营业额
        ReportFormReq req2 = new ReportFormReq();
        req2.setStoreId(loginUser.getStoreId());
        req2.setDateFlag(String.valueOf(leastYear));
        List<ReportLineDataBean> dayHoursSaleIncomeUp1List = reportFormMapper.reportYearMonthSaleIncome(req2);
        reportForm.setSaleIncomeUp1List(dayHoursSaleIncomeUp1List);
        // 获取每月支出
        List<ReportLineDataBean> dayHoursSaleOutcomeUp1List = reportFormMapper.reportYearMonthSaleOutcome(req2);
        reportForm.setSaleOutcomeUp1List(dayHoursSaleOutcomeUp1List);
        // 获取每月利润
        List<ReportLineDataBean> saleProfitUp1List = reportFormMapper.reportYearMonthSaleProfit(req2);
        reportForm.setSaleProfitUp1List(saleProfitUp1List);

        // ----------------------------------------------- 前两年营收及成本
        // 获取每月营业额
        ReportFormReq req3 = new ReportFormReq();
        req3.setStoreId(loginUser.getStoreId());
        req3.setDateFlag(String.valueOf(leastTwoYear));
        List<ReportLineDataBean> dayHoursSaleIncomeUp2List = reportFormMapper.reportYearMonthSaleIncome(req3);
        reportForm.setSaleIncomeUp2List(dayHoursSaleIncomeUp2List);
        // 获取每月支出
        List<ReportLineDataBean> dayHoursSaleOutcomeUp2List = reportFormMapper.reportYearMonthSaleOutcome(req3);
        reportForm.setSaleOutcomeUp2List(dayHoursSaleOutcomeUp2List);
        // 获取每月利润
        List<ReportLineDataBean> saleProfitUp2List = reportFormMapper.reportYearMonthSaleProfit(req3);
        reportForm.setSaleProfitUp2List(saleProfitUp2List);

        return reportForm;
    }
}
